// standard libraries
#include <string>
#include <cstdio>

// wii libraries
#include <fat.h>

// custom libraries
#include "..\..\include\file\filebase.h" // matching header


// ==================================
//    CONSTRUCTORS and DESTRUCTORS
// ==================================

FileBase::FileBase() 
{
    file_ = 0;
}

FileBase::~FileBase() 
{
	if (is_open())
		close();
}


// ==================================
//            FUNCTIONS
// ==================================

void FileBase::open_as(std::string path, OpenMode openMode) 
{
	if (!is_open()) {
		if (fatInitDefault()) {
			// Array of strings representing various file acces modes for the
			// 'fopen' function. Order in array must match order in the 
			// FileBase::OpenMode enum.
			std::string OpenModes[FileBase::OPEN_MODE_SIZE] = {
				"ab+", "rb", "wb"
			};

			std::string fam = OpenModes[openMode];
			file_ = fopen(path.c_str(), fam.c_str());
		}
	}
}

void FileBase::close()
{
	if (is_open()) {
		fclose(file_);
		file_ = 0;
	}
}

bool FileBase::is_open()
{
	return file_ != 0;
}
